package com.fyx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fyx.entity.*;
import com.fyx.mapper.*;
import com.fyx.service.IRunDataService;
import com.fyx.until.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 方扬秀
 * @since 2020-12-22
 */
@Service
public class RunDataServiceImpl extends ServiceImpl<AllFeeMapper, AllFee> implements IRunDataService {
    @Autowired
    private AllFeeMapper allFeeMapper;

    @Autowired
    private MeituanReStayMapper meituanReStayMapper;

    @Autowired
    private MeituanMaiInfoMapper meituanMaiInfoMapper;

    @Override
    public Map allFeeActiveUser(AllFee allfee, Pageable pageable) {
        Page<AllFee> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
        QueryWrapper<AllFee> wrapper =new QueryWrapper<>();
        if(allfee.getQTime()!=null){
            wrapper.ge("time",allfee.getQTime().get(0).getTime()/1000);
            wrapper.le("time",allfee.getQTime().get(1).getTime()/1000);
        }
        wrapper.orderBy(true,false,"time");
        IPage<AllFee> ipage = allFeeMapper.selectPage(page,wrapper);
        List<AllFee> list = ipage.getRecords();
        long total = ipage.getTotal();
        Map map =new HashMap();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

    @Autowired
    private LostGoldMapper lostGoldMapper;

    @Override
    public Map gamelostGold(LostGold lostGold, Pageable pageable) {
        Page<LostGold> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
        QueryWrapper<LostGold> wrapper =new QueryWrapper<>();
        if(lostGold.getQTime()!=null){
            wrapper.ge("time",lostGold.getQTime().get(0).getTime()/1000);
            wrapper.le("time",lostGold.getQTime().get(1).getTime()/1000);
        }
        wrapper.orderBy(true,false,"time");
        IPage<LostGold> ipage = lostGoldMapper.selectPage(page,wrapper);
        List<LostGold> list = ipage.getRecords();
        long total = ipage.getTotal();
        Map map =new HashMap();
        map.put("list",list);
        map.put("total",total);
        return map;
    }


    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Map runData(OperationRecEntity operationRecEntity, Pageable pageable) {
        Query query = new Query();
        query.skip((pageable.getPageNumber())*pageable.getPageSize());
        query.limit(pageable.getPageSize());
        Criteria criteria = new Criteria();
        criteria.and("mode_name").is("operation");
        criteria.and("day").ne(DateUtil.getDayBegin().getTime()/1000);
        if (operationRecEntity.getQTime()!=null){
            criteria.andOperator(Criteria.where("day").gte(operationRecEntity.getQTime().get(0).getTime()/1000).lte(operationRecEntity.getQTime().get(1).getTime()/1000));
        }
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Direction.DESC,new String[]{"day"}));
        List<OperationRecEntity> list = mongoTemplate.find(query, OperationRecEntity.class);
        long total = mongoTemplate.count(query, OperationRecEntity.class);

        Map map =new HashMap();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

    @Autowired
    private CumRechargeMapper cumRechargeMapper;

    @Override
    public  Map cumRecharge(CumRechargeEntity cumRechargeEntity, Pageable pageable){
        Page<CumRechargeEntity> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
        QueryWrapper<CumRechargeEntity> wrapper =new QueryWrapper<>();
        if(cumRechargeEntity.getQTime()!=null){
            wrapper.ge("time",cumRechargeEntity.getQTime().get(0).getTime()/1000);
            wrapper.le("time",cumRechargeEntity.getQTime().get(1).getTime()/1000);
        }
        if(cumRechargeEntity.getChannel()!=null){
            wrapper.eq("channel",cumRechargeEntity.getChannel());
        }else{
            wrapper.eq("channel","total").or().isNull("channel");

        }
        if(cumRechargeEntity.getTime()!=null&&cumRechargeEntity.getTime()>0){
            wrapper.eq("time",cumRechargeEntity.getTime());
            wrapper.ne("channel","total");
        }
        wrapper.orderBy(true,false,"time");
        IPage<CumRechargeEntity> ipage = cumRechargeMapper.selectPage(page,wrapper);
        List<CumRechargeEntity> list = ipage.getRecords();
        long total = ipage.getTotal();
        Map map =new HashMap();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

    @Override
    public List<CumRechargeEntity> getCumRecharge(Integer time){
        QueryWrapper<CumRechargeEntity> wrapper =new QueryWrapper<>();
        if(time!=null&&time>0){
            wrapper.eq("time",time);
            wrapper.ne("channel","total");
        }
        wrapper.orderBy(true,false,"id");
        List<CumRechargeEntity> list = cumRechargeMapper.selectList(wrapper);
        if(list.size()>0){
            for(CumRechargeEntity cumRechargeEntity:list){
                cumRechargeEntity.setHasChildren(false);
            }
        }
        return list;
    }
    @Override
    public Map getMeituMai(MeituanMaiInfo meituanMaiInfo, Pageable pageable){
        Page<MeituanMaiInfo> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
        QueryWrapper<MeituanMaiInfo> wrapper =new QueryWrapper<>();
        if(meituanMaiInfo.getQTime()!=null){
            wrapper.ge("time",meituanMaiInfo.getQTime().get(0).getTime()/1000);
            wrapper.le("time",meituanMaiInfo.getQTime().get(1).getTime()/1000);
        }
        wrapper.orderBy(true,false,"time");
        IPage<MeituanMaiInfo> ipage = meituanMaiInfoMapper.selectPage(page,wrapper);
        List<MeituanMaiInfo> list = ipage.getRecords();
        long total = ipage.getTotal();
        Map map =new HashMap();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

    @Override
    public Map getMeituTen(MeituanReStay meituanReStay, Pageable pageable){
        Page<MeituanReStay> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
        QueryWrapper<MeituanReStay> wrapper =new QueryWrapper<>();
        if(meituanReStay.getQTime()!=null){
            wrapper.ge("time",meituanReStay.getQTime().get(0).getTime()/1000);
            wrapper.le("time",meituanReStay.getQTime().get(1).getTime()/1000);
        }
        wrapper.orderBy(true,false,"time");
        IPage<MeituanReStay> ipage = meituanReStayMapper.selectPage(page,wrapper);
        List<MeituanReStay> list = ipage.getRecords();
        long total = ipage.getTotal();
        Map map =new HashMap();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

    @Autowired
    private  BigOrSmallMapper bigOrSmallMapper;

    @Override
    public Map bigOrSmall(BigOrSmallEntity bigOrSmallEntity, Pageable pageable){
        Page<BigOrSmallEntity> page = new Page<>(pageable.getPageNumber()+1,pageable.getPageSize());
        QueryWrapper<BigOrSmallEntity> wrapper =new QueryWrapper<>();
        if(bigOrSmallEntity.getQTime()!=null){
            wrapper.ge("time",bigOrSmallEntity.getQTime().get(0).getTime()/1000);
            wrapper.le("time",bigOrSmallEntity.getQTime().get(1).getTime()/1000);
        }
        wrapper.eq("flag",bigOrSmallEntity.getFlag());
        wrapper.orderBy(true,false,"time");
        IPage<BigOrSmallEntity> ipage = bigOrSmallMapper.selectPage(page,wrapper);
        List<BigOrSmallEntity> list = ipage.getRecords();
        if(list.size()>0){
            for (BigOrSmallEntity bigOrSmallEntity1:list){
                bigOrSmallEntity1.setValues(JSONObject.parseObject(bigOrSmallEntity1.getTheValue()));
            }
        }
        long total = ipage.getTotal();
        Map map =new HashMap();
        map.put("list",list);
        map.put("total",total);
        return map;
    }

}
